<?php

	function getRanking()
	{
		@$type = $_GET['type'];

		if($type == 'kd' && !isset($_GET['search']) || !isset($type) && !isset($_GET['search']) || isset($type))
		{
			$q = mysql_query("SELECT *, (characters.wins/characters.losses) AS n FROM account, characters WHERE characters.aid = account.id AND account.grade > 0 ORDER BY n DESC LIMIT 50");
		}
		else if($type == 'level' && !isset($_GET['search']))
		{
			$q = mysql_query("SELECT * FROM account, characters WHERE characters.aid = account.id AND account.grade > 0 ORDER BY characters.level DESC LIMIT 50");
		}
		else if($type == 'kill' && !isset($_GET['search']))
		{
			$q = mysql_query("SELECT * FROM account, characters WHERE characters.aid = account.id AND account.grade > 0 ORDER BY characters.wins DESC LIMIT 50");
		}
		else if($type == 'death' && !isset($_GET['search']))
		{
			$q = mysql_query("SELECT * FROM account, characters WHERE characters.aid = account.id AND account.grade > 0 ORDER BY characters.losses DESC LIMIT 50");
		}
		else if($type == 'mission' && !isset($_GET['search']))
		{
			$q = mysql_query("SELECT * FROM account, characters WHERE characters.aid = account.id AND account.grade > 0 ORDER BY characters.finished_missions DESC LIMIT 50");
		}
		else if($type == 'money' && !isset($_GET['search']))
		{
			$q = mysql_query("SELECT * FROM account, characters WHERE characters.aid = account.id AND account.grade > 0 ORDER BY account.pollars+account.pank DESC LIMIT 50");
		}
		
		$i = 1;
		echo '<table>';
		echo '<tr>
				<td width="20"><b>#</b></td>
				<td width="80"><b>Name</b></td>
				<td width="30"><b>Level</b></td>
				<td width="100"><b>Experience</b></td>
				<td width="70"><b>Missions</b></td>
				<td width="40"><b>Kills</b></td>
				<td width="60"><b>Deaths</b></td>
				<td width="100"><b>Pollars</b></td>
				<td><b>K/D Ratio</b></td>
			</tr>';

		if(!isset($_GET['search']))
		{
			while($f = mysql_fetch_array($q))
			{
				$wins = $f['wins'];
				$losses = $f['losses'];

				if($losses == 0)
				{
					if($wins == 0)
					{
						$kd = -0;
					}
					else
					{
						$kd = $wins;
					}
				}
				else if($wins == 0)
				{
					if($losses == 0)
					{
						$kd = 0;
					}
					else
					{
						$kd = 1/$losses;
					}
				}
				else
				{
					$kd = $wins/$losses;
				}

				echo '
						<tr>
							<td>'.$i.'</td>';
							
							if(!isset($_SESSION['user']))
							{
								echo '<td>'.$f['name'].'</td>';
							}
							else if($f['username'] != $_SESSION['user'])
							{
								echo '<td><a href="?page=profile&id='.$f['id'].'">'.$f['name'].'</a></td>';
							}
							else
							{
								echo '<td><a href="?page=profile">'.$f['name'].'</a></td>';
							}

							echo '
							<td>'.$f['level'].'</td>
							<td>'.$f['xp'].'</td>
							<td>'.$f['finished_missions'].'</td>
							<td>'.$f['wins'].'</td>
							<td>'.$f['losses'].'</td>
							<td>'.($f['pollars']+$f['pank']).'</td>
							<td>'.round($kd, 2).'</td>
						</tr>
						';
				$i++;
			}
			echo '</table>';
		
		}
		else
		{
			$name = $_GET['search'];
			$q = mysql_query("SELECT * FROM account, characters WHERE characters.aid = account.id AND account.grade > 0 AND characters.name LIKE '%$name%' ORDER BY characters.name");
			
			while($f = mysql_fetch_array($q))
			{
				$wins = $f['wins'];
				$losses = $f['losses'];

				if($losses == 0)
				{
					if($wins == 0)
					{
						$kd = -0;
					}
					else
					{
						$kd = $wins;
					}
				}
				else if($wins == 0)
				{
					if($losses == 0)
					{
						$kd = 0;
					}
					else
					{
						$kd = 1/$losses;
					}
				}
				else
				{
					$kd = $wins/$losses;
				}

				echo '
						<tr>
							<td>'.$i.'</td>';
							
							if(!isset($_SESSION['user']))
							{
								echo '<td>'.$f['name'].'</td>';
							}
							else if($f['username'] != $_SESSION['user'])
							{
								echo '<td><a href="?page=profile&id='.$f['id'].'">'.$f['name'].'</a></td>';
							}
							else
							{
								echo '<td><a href="?page=profile">'.$f['name'].'</a></td>';
							}

							echo '
							<td>'.$f['level'].'</td>
							<td>'.$f['xp'].'</td>
							<td>'.$f['finished_missions'].'</td>
							<td>'.$f['wins'].'</td>
							<td>'.$f['losses'].'</td>
							<td>'.($f['pollars']+$f['pank']).'</td>
							<td>'.round($kd, 2).'</td>
						</tr>
						';
				$i++;
			}
			echo '</table>';
		}


	}

	function getTop5($type = 'level')
	{
		if($type == 'level')
		{
			$q = mysql_query("SELECT * FROM account, characters WHERE characters.aid = account.id AND account.grade > 0 ORDER BY characters.level DESC LIMIT 5");
			$name  = 'Level';
		}
		else if($type == 'money')
		{
			$q = mysql_query("SELECT * FROM account, characters WHERE characters.aid = account.id AND account.grade > 0 ORDER BY account.pollars+account.pank DESC LIMIT 5");
			$name = 'Pollar';
		}
		else if($type == 'kd')
		{
			$q = mysql_query("SELECT *, (characters.wins/characters.losses) AS n FROM account, characters WHERE characters.aid = account.id AND account.grade > 0 ORDER BY n DESC LIMIT 5");
			$name = 'K/d';
		}

		$i = 1;
		
		echo '<table class="option">
			<td width="10">#</td>
			<td width="110">Name</td>
			<td>'.$name.'</td>
		</table>
		';

		while($f = mysql_fetch_array($q))
			{
				$wins = $f['wins'];
				$losses = $f['losses'];

				if($losses == 0)
				{
					if($wins == 0)
					{
						$kd = -0;
					}
					else
					{
						$kd = $wins;
					}
				}
				else if($wins == 0)
				{
					if($losses == 0)
					{
						$kd = 0;
					}
					else
					{
						$kd = 1/$losses;
					}
				}
				else
				{
					$kd = $wins/$losses;
				}

				echo '
						<table class="option">
						<tr>
							<td width="10">'.$i.'</td>';
							
							if(!isset($_SESSION['user']))
							{
								echo '<td width="110">'.$f['name'].'</td>';
							}
							else if($f['username'] != $_SESSION['user'])
							{
								echo '<td width="110"><a href="?page=profile&id='.$f['id'].'">'.$f['name'].'</a></td>';
							}
							else
							{
								echo '<td width="110"><a href="?page=profile">'.$f['name'].'</a></td>';
							}

							if($type == 'money')
							{
								$money = getPollars($f['aid'])+getPankPollars($f['aid']);
								if($money > 1000000)
								{
									$money = floor($money/1000000);
									$money = $money.'M';
								}
								else if($money > 100000)
								{
									$money = floor($money/1000);
									$money = $money.'K';
								}
								echo '<td>'.$money.'</td>';
							}
							else if($type == 'kd')
							{
								echo '<td>'.round($kd, 2).'</td>';
							}
							else
							{
								echo '<td>'.$f['level'].'</td>';
							}
							


							echo '</tr>';
						echo '</table>';
				$i++;
			}
			
	}
		



?>